﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.IO;
using System.Web.Compilation;
using System.Web.WebPages;

namespace V5_EShop {
    public partial class Index : BasicPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string pageName = "index";
                string rPageName = InputHelper.SetString(Request["t"]);
                if (!string.IsNullOrEmpty(rPageName)) {
                    pageName = rPageName;
                }
                string templateDir = "default";
                if (!string.IsNullOrEmpty(pageName)) {
                    V5.ViewData["ClassID"] = Request["ClassID"];
                    V5.ViewData["ID"] = Request["ID"];
                    V5.ViewData["PageID"] = Request["PageID"];
                    V5.ViewData["String"] = Request["String"];
                    templateDir = V5.Config.GetConfigValue("TemplateDir");
                    pageName = V5.Url.SplitController(pageName);
                    string PathPage = "/Template/" + templateDir + "/" + pageName + ".cshtml";
                    var path = System.IO.Path.Combine(Request.ApplicationPath, PathPage);
                    var type = BuildManager.GetCompiledType(path);
                    var script = Activator.CreateInstance(type) as System.Web.WebPages.WebPage;
                    var stringWriter = new StringWriter();
                    script.VirtualPath = "~" + PathPage;
                    script.ExecutePageHierarchy(new WebPageContext(
                            new HttpContextWrapper(HttpContext.Current), script, null
                            ), stringWriter);
                    Response.Write(stringWriter.ToString());
                    Response.End();
                }
            }
        }


        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
        }
        protected override void Render(HtmlTextWriter writer) {
            base.Render(writer);
        }

        protected void Page_Error(object sender, EventArgs e) {
            Exception ex = Server.GetLastError();
            if (ex is HttpRequestValidationException) {
                Response.Write(ex.Message + "==" + ex.InnerException);
                Server.ClearError(); 
            }
        }

    }
}